package com.qut.graduate.busrtquery.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.busline.BusLineSearchOption;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.utils.DistanceUtil;
import com.qingmei2.library.SlideBottomLayout;
import com.qut.graduate.busrtquery.BRTQCache;
import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.BaseActivity;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.adapter.LineQueryAdapter;
import com.qut.graduate.busrtquery.bean.BusInfoLineQuery;
import com.qut.graduate.busrtquery.bean.BusLineInfo;
import com.qut.graduate.busrtquery.bean.BusStation;
import com.qut.graduate.busrtquery.bean.Route;
import com.qut.graduate.busrtquery.bean.User;
import com.qut.graduate.busrtquery.greendao.bean.StationInfo;
import com.qut.graduate.busrtquery.greendao.bean.StationInfoCollect;
import com.qut.graduate.busrtquery.greendao.controller.DBController;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;
import com.qut.graduate.busrtquery.listener.MyOrientationListener;
import com.qut.graduate.busrtquery.utils.baidumap.BaiduMapUtils;
import com.qut.graduate.busrtquery.utils.baidumap.BusLineOverlay;
import com.qut.graduate.busrtquery.utils.common.FormatUtils;
import com.qut.graduate.busrtquery.utils.common.GsonUtils;
import com.qut.graduate.busrtquery.utils.common.LogUtils;
import com.qut.graduate.busrtquery.utils.net.HttpUtil;
import com.qut.graduate.busrtquery.utils.uiabout.ScreenUtils;
import com.qut.graduate.busrtquery.utils.uiabout.StatusBarUtils;
import com.qut.graduate.busrtquery.utils.uiabout.ToastColorfulUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.BmobUser;

public class StationQuerySuccessActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "StationQuerySuccessActivity";
    private boolean isFirstIn = true; // 是否是第一次定位，若是则将地图切换到定位位置
    private MyOrientationListener myOrientationListener;
    private MyLocationConfiguration.LocationMode locationMode; // 定位图层显示方式,地图SDK支持三种定位模式：NORMAL（普通态）, FOLLOWING（跟随态）, COMPASS（罗盘态）
    private LatLng mCurrentLatLng; // 当前经纬度对应位置
    private float mCurrentX; // 当前方向
    private float mCurrentAccracy; // 当前定位精度
    private String currentCity = "";
    private double mLatitude; // 当前纬度
    private double mLongitude; // 当前经度
    private Marker marker;
    private BusLineResult route = null; // 保存驾车/步行路线数据的变量，供浏览节点时使用
    private List<String> busLineIDList = null;
    private int busLineIndex = 0;
    private BusLineOverlay overlay; // 公交路线绘制对象
    private PoiSearch mSearch = null; // 搜索模块，也可去掉地图模块独立使用
    private BusLineSearch mBusLineSearch = null;  // 搜索公交线路
    //    private PoiInfo mPoiInfo;
    private StationInfo mStationInfo;
    private List<BusInfoLineQuery> mBusInfoLineQueryList;
    private Map<String, Route> mApiRoutesMap;
    private boolean isCollect = false; // 判断是否收藏，true为收藏，false为未收藏

    private BaiduMap mBaiduMap;
    private MapView mMapView = null;
    private Button mCollectBtn;
    private ImageButton mCollectBtn2;
    private ImageButton mBackImgBtn;
    private ImageButton mTrafficImgBtn;
    private ImageButton mCoverageImgBtn;
    private ImageButton mLocationImgBtn;
    private LinearLayout mCoverageLayout;
    private ImageButton m2dMapImgBtn;
    private ImageButton mSiteMapImgBtn;
    private ImageButton m3dMapImgBtn;
    private TextView mPlaceInfoName;
    private TextView mPlaceInfoDistance;
    private TextView mPlaceInfoAddress;
    private ImageButton mZoomIn;
    private ImageButton mZoomOut;
    private TextView mContent;
    private SlideBottomLayout mSlideLayout;
    private RecyclerView mBusRecyclerView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_station_query_sucess;
    }

    @Override
    protected void init() {
        super.init();
        mContext = this;
        initView();
        initEvent();
    }

    private void initEvent() {
        mSearch = PoiSearch.newInstance();
        mSearch.setOnGetPoiSearchResultListener(this);
        mBusLineSearch = BusLineSearch.newInstance();
        mBusLineSearch.setOnGetBusLineSearchResultListener(this);
        busLineIDList = new ArrayList<String>();
        overlay = new BusLineOverlay(mBaiduMap);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            mStationInfo = bundle.getParcelable("stationInfo");
        }
        if (mStationInfo != null) {
            judgeLoginCollectStatus();
            mPlaceInfoName.setText(mStationInfo.getName());
            mPlaceInfoAddress.setText(mStationInfo.getAddress());
            currentCity = mStationInfo.getCity();
            markThePoint(mStationInfo);
            mApiRoutesMap = BRTQCache.getInstance().getmApiRoutesMap();
            mBusInfoLineQueryList = new ArrayList<>();
            if (mStationInfo.getAddress().contains(";")){
                String[] busNames = mStationInfo.getAddress().split(";");
                mContent.setText("本站共" + busNames.length + "条公交线路");
                for (int i = 0; i < busNames.length; i++) {
                    BusInfoLineQuery busInfoLineQuery = new BusInfoLineQuery();
//                busInfoLineQuery.setName("1路");
//                busInfoLineQuery.setRid("1");
                    busInfoLineQuery.setName(busNames[i]);
                    Route route = mApiRoutesMap.get(busNames[i]);
                    if (route != null) {
                        busInfoLineQuery.setRid(route.getRouteid());
                    }
                    mBusInfoLineQueryList.add(busInfoLineQuery);
                }
            }else mContent.setText("该地点不是公交站点");
            if (mBusInfoLineQueryList.size() == 0) {
                ToastColorfulUtils.info("未查到该站点相关线路信息");
            }
            mBusRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            LineQueryAdapter lineQueryAdapter = new LineQueryAdapter(this, mBusInfoLineQueryList);
            mBusRecyclerView.setAdapter(lineQueryAdapter);
            lineQueryAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    if (mApiRoutesMap != null && mApiRoutesMap.containsKey(mBusInfoLineQueryList.get(position).getName())) {
//                    if (mApiRoutesMap != null && mApiRoutesMap.containsKey("1路")) {
                        BRTQCache.getInstance().setBusInfoLineQuery(mBusInfoLineQueryList.get(position));
                        Route route = mApiRoutesMap.get(mBusInfoLineQueryList.get(position).getName());
//                        String url = BRTQConstant.URL_BUS_LINE_INFO.concat("1");
                        if (route != null) {
                            String url = BRTQConstant.URL_BUS_LINE_INFO.concat(route.getRouteid());
                            LogUtils.d("----StationQuerySuccessActivity  url:" + url, getClass());
                            HttpUtil.getInstance().getAsyn(url, StationQuerySuccessActivity.this, BRTQConstant.HTTP_TAG_BUS_LINE_INFO);
                        } else {
                            searchBusLine(mBusInfoLineQueryList.get(position).getName());
                            ToastColorfulUtils.info("未查到该公交线路实时信息");
                        }
                    } else {
                        searchBusLine(mBusInfoLineQueryList.get(position).getName());
                        ToastColorfulUtils.info("未查到该公交线路实时信息");
                    }
                }
            });
        }
        initLocation();
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                if (mCoverageLayout.getVisibility() == View.VISIBLE) {
                    mCoverageLayout.setVisibility(View.GONE);
                } else {
                    if (mStationInfo != null) markThePoint(mStationInfo);
                }
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                BaiduMapUtils.startScaleAnimation(marker);
                if (overlay.getmOverlayList() != null && overlay.getmOverlayList().contains(marker)) {
                    int index = overlay.getmOverlayList().indexOf(marker);
                    if (route.getStations() != null && route.getStations().get(index) != null) {
                        nodeClick(index);
                    }
                }
                return true;
            }
        });
    }

    public void judgeLoginCollectStatus() {
        User user = BmobUser.getCurrentUser(User.class);
        if (user != null) {
            List<StationInfoCollect> stationInfoCollects = BRTQCache.getInstance().getStationInfoCollectList();
            for (int i = 0; i < stationInfoCollects.size(); i++) {
                if (mStationInfo.getName().equals(stationInfoCollects.get(i).getName())) {
                    isCollect = true;
                    break;
                } else isCollect = false;
            }
            setWhichCollectBtnVisibility(isCollect);
        } else
            setWhichCollectBtnVisibility(false);
    }

    // 点击站点后显示该站点所在位置
    public void nodeClick(int position) {
        TextView popupText = new TextView(mContext);
        popupText.setBackgroundResource(R.mipmap.popup);
        popupText.setTextColor(0xff000000);
        try {
            if (route != null) {
                // 移动到指定索引的坐标
                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(route.getStations().get(position).getLocation()));
                popupText.setText(route.getStations().get(position).getTitle()); // 弹出泡泡
                mBaiduMap.showInfoWindow(new InfoWindow(popupText, route.getStations().get(position).getLocation(), 10));
            }
        } catch (Exception e) {
            LogUtils.d("----BusLineOverlay  nodeClick异常：" + e, getClass());
        }
    }

    @Override
    public void onHttpSuccess(String json, int tag) {
        String responseBusInfo = FormatUtils.unicodeToString(json);
        if (tag == BRTQConstant.HTTP_TAG_BUS_LINE_INFO) {
            LogUtils.d("----站点查询成功界面 HttpSuccess  BUS_LINE_INFO JSON字符串" + responseBusInfo, getClass());
            BusInfoLineQuery busInfoLineQuery = BRTQCache.getInstance().getBusInfoLineQuery();
            Route route = mApiRoutesMap.get(busInfoLineQuery.getName());
            BusLineInfo busLineInfo = GsonUtils.getInstance().getBusInfo(responseBusInfo, busInfoLineQuery);
            List<BusStation> busStationList = GsonUtils.getInstance().getBusStation(responseBusInfo, busInfoLineQuery);
            LogUtils.d("----站点查询成功界面 跳转到公交路线实时信息界面之前----city: " + currentCity +
                    "----busName: " + route.getRoutename(), getClass());
            gotoBusRouteRealTimeInfo(route, busLineInfo, busStationList);
        }
    }

    @Override
    public void onHttpFailed(String errorMsg, int tag) {
        LogUtils.d("----站点查询成功界面 HttpFailed  errorMsg  " + errorMsg);
    }

    /**
     * 跳转到公交路线实时信息界面
     *
     * @param route
     */
    public void gotoBusRouteRealTimeInfo(Route route, BusLineInfo busLineInfo, List<BusStation> busStationList) {
        Intent intent = new Intent(this, BusRouteRealTimeInfoActivity.class);
        Bundle bundle = new Bundle();
        if (currentCity != null && !currentCity.isEmpty()) {
//            bundle.putString("city", currentCity);
            bundle.putString("city", "青岛");
        } else bundle.putString("city", "青岛");
        bundle.putParcelable("route", route);
        bundle.putParcelable("busLineInfo", busLineInfo);
        bundle.putParcelableArrayList("busStationList", (ArrayList<? extends Parcelable>) busStationList);
        LogUtils.d("----站点查询成功界面跳转到公交路线实时信息界面之前----city: " + currentCity + "----busName: " + route.getRoutename(), getClass());
        intent.putExtras(bundle);
        startActivity(intent);
    }

    private void markThePoint(StationInfo stationInfo) {
//        mBaiduMap.clear();
        LatLng latLng = new LatLng(stationInfo.getLatitude(), stationInfo.getLongitude());
        View view = View.inflate(StationQuerySuccessActivity.this, R.layout.item_map_info_icon, null);
        ImageView iconView = view.findViewById(R.id.info_icon_imageView); // 填充数据
        TextView nameView = view.findViewById(R.id.info_icon_index_textView);
        iconView.setImageResource(R.mipmap.icon_gcoding);
//       iconView.setImageResource(R.mipmap.icon_map_info);
        BitmapDescriptor mMarker = BitmapDescriptorFactory.fromView(view); // 构建BitmapDescriptor
        // 准备 marker option 添加 marker 使用
        MarkerOptions markerOptions = new MarkerOptions().icon(mMarker).position(latLng).animateType(MarkerOptions.MarkerAnimateType.grow);
        // 获取添加的 marker 这样便于后续的操作
        marker = (Marker) mBaiduMap.addOverlay(markerOptions);
        MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
        mBaiduMap.animateMapStatus(msu);

    }

    // 定位
    private void initLocation() {
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        mBaiduMap.setMyLocationEnabled(true);
        myOrientationListener = new MyOrientationListener(this);
        myOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() {
            @Override
            public void onOrientationChanged(float x) {
                mCurrentX = x;
                MyLocationData data = new MyLocationData.Builder()
                        .direction(mCurrentX)
                        .accuracy(mCurrentAccracy)
                        .latitude(mLatitude)
                        .longitude(mLongitude)
                        .build();
                mBaiduMap.setMyLocationData(data);
            }
        });
    }

    @Override
    // 定位改变回调，在需要的activity中重写该方法
    public void onLocationChange(BDLocation bdLocation) {
        if (bdLocation != null) {
            mLatitude = bdLocation.getLatitude();
            mLongitude = bdLocation.getLongitude();
            mCurrentLatLng = new LatLng(mLatitude, mLongitude);
            mCurrentAccracy = bdLocation.getRadius();
//            currentCity = bdLocation.getCity();
            MyLocationConfiguration configuration = new MyLocationConfiguration(locationMode, true, null);
            mBaiduMap.setMyLocationConfiguration(configuration);
            if (isFirstIn) {
//                BaiduMapUtils.getMyLocation(mBaiduMap, mCurrentLatLng);
                isFirstIn = false;
                if (mStationInfo != null && mCurrentLatLng != null) {
                    LatLng latLng = new LatLng(mStationInfo.getLatitude(), mStationInfo.getLongitude());
                    double dis = DistanceUtil.getDistance(mCurrentLatLng, latLng);
                    String distance = "";
                    if (dis < 1000) distance = String.valueOf((int) dis) + " m";
                    else distance = String.format("%.2f", (dis / 1000)) + " km";
                    mPlaceInfoDistance.setText(distance);
                }
            }
        }
    }

    // 发起poi检索
    public void searchBusLine(String busName) {
        busLineIDList.clear();
        busLineIndex = 0;
        // 发起poi检索，从得到所有poi中找到公交线路类型的poi，再使用该poi的uid进行公交详情搜索
        if (currentCity != null && !currentCity.equals(""))
            mSearch.searchInCity((new PoiCitySearchOption()).city(currentCity).keyword(busName));
        else
            mSearch.searchInCity((new PoiCitySearchOption()).city("青岛").keyword(busName));
    }

    @Override
    public void onGetPoiResult(PoiResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            LogUtils.d("----站点查询成功界面 onGetPoiResult： 抱歉，未找到结果");
            return;
        }
        busLineIDList.clear(); // 遍历所有poi，找到类型为公交线路的poi
        for (PoiInfo poi : result.getAllPoi()) {
            busLineIDList.add(poi.uid);
        }
        mBusLineSearch.searchBusLine((new BusLineSearchOption().city(currentCity).uid(busLineIDList.get(0))));
        route = null;
    }

    @Override
    public void onGetBusLineResult(BusLineResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            LogUtils.d("----站点查询成功界面 onGetBusLineResult： 抱歉，未找到结果");
            return;
        }
//        mBaiduMap.clear();
        route = result;
        overlay.removeFromMap();
        overlay.setData(result);
        overlay.addToMap();
        overlay.zoomToSpan();
        mSlideLayout.hide();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.station_query_success_back_imgbtn:
                finish();
                break;
            case R.id.station_query_success_traffic_imgbtn:
                BaiduMapUtils.getTrafficCondition(mBaiduMap, mTrafficImgBtn);
                break;
            case R.id.location_img_btn:
//                changeLocationType();
                BaiduMapUtils.getMyLocation(mBaiduMap, mCurrentLatLng);
                break;
            case R.id.station_query_success_coverage_imgbtn:
                if (mCoverageLayout.getVisibility() == View.GONE)
                    mCoverageLayout.setVisibility(View.VISIBLE);
                else mCoverageLayout.setVisibility(View.GONE);
                break;
            case R.id.station_query_success_map_2d_img_btn:
                BaiduMapUtils.changeMapType(mBaiduMap, BaiduMap.MAP_TYPE_NORMAL, 45, 15.0f, mCoverageLayout);
                break;
            case R.id.station_query_success_map_site_img_btn:
                BaiduMapUtils.changeMapType(mBaiduMap, BaiduMap.MAP_TYPE_SATELLITE, 45, 15.0f, mCoverageLayout);
                break;
            case R.id.station_query_success_map_3d_img_btn:
                BaiduMapUtils.changeMapType(mBaiduMap, BaiduMap.MAP_TYPE_NORMAL, -90, 19.0f, mCoverageLayout);
//                locationMode = MyLocationConfiguration.LocationMode.COMPASS;
//                mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(locationMode, true, null));
//                changeLocationType();
                break;
            case R.id.zoom_in:
                BaiduMapUtils.zoomIn(mBaiduMap, mZoomOut, mZoomIn);
                break;
            case R.id.zoom_out:
                BaiduMapUtils.zoomOut(mBaiduMap, mZoomOut, mZoomIn);
                break;
            case R.id.station_query_success_collectBtn:
                User user = BmobUser.getCurrentUser(User.class);
                if (user != null) {
                    if (isCollect) { // 已经收藏了从内存和数据库中删除
                        deleteStationInfoCollect();
                        return; // 如果这里不return，会继续执行下面的代码
                    } else {
                        isCollect = true;
                        setWhichCollectBtnVisibility(isCollect);
                        if (mStationInfo != null) {
                            StationInfoCollect stationInfoCollect = new StationInfoCollect(mStationInfo);
                            BRTQCache.getInstance().getStationInfoCollectList().add(stationInfoCollect);
                            deleteRepeatStationInfoCollect(BRTQCache.getInstance().getStationInfoCollectList());
                            DBController.getInstance().insertTable(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT, stationInfoCollect);
                            LogUtils.d("----站点查询成功界面 数据库插入收藏表该收藏站点：" + stationInfoCollect);
                        }
                    }
                } else
                    ToastColorfulUtils.info("登录后才能收藏当前站点");
                break;
            case R.id.station_query_success_collectBtn2:
                deleteStationInfoCollect();
                break;
        }
    }

    public void deleteStationInfoCollect() {
        isCollect = false;
        setWhichCollectBtnVisibility(isCollect);
        List<StationInfoCollect> stationInfoCollects = BRTQCache.getInstance().getStationInfoCollectList();
        LogUtils.d("----站点查询成功界面 所有收藏站点 stationInfoCollects： " + stationInfoCollects);
        for (int i = 0; i < stationInfoCollects.size(); i++) {
            if (stationInfoCollects.get(i).getName().equals(mStationInfo.getName())) {
                LogUtils.d("----站点查询成功界面 数据库删除收藏表该收藏站点：" + stationInfoCollects.get(i));
                stationInfoCollects.remove(i);
            }
        }
        List<StationInfoCollect> stationInfoCollectList = (ArrayList<StationInfoCollect>) DBController.getInstance().
                queryObjectByName(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT, mStationInfo.getName());
        DBController.getInstance().deleteTable(BRTQConstant.DB_TABLE_TYPE_STATION_INFO_COLLECT,
                stationInfoCollectList.get(0));
    }

    public void deleteRepeatStationInfoCollect(List<StationInfoCollect> busLines) {
        for (int i = 0; i < busLines.size(); i++) {
            for (int j = 0; j < i; j++) {
                if (busLines.get(i).getName().equals(busLines.get(j).getName())) {
                    busLines.remove(i);
                    i = i - 1;
                    break;
                }
            }
        }
    }

    /**
     * 只是设置显示还是不显示收藏的按钮
     *
     * @param collect
     */
    public void setWhichCollectBtnVisibility(boolean collect) {
        if (collect) {
            mCollectBtn.setVisibility(View.GONE);
            mCollectBtn2.setVisibility(View.VISIBLE);
        } else {
            mCollectBtn.setVisibility(View.VISIBLE);
            mCollectBtn2.setVisibility(View.GONE);
        }
    }

    private void initView() {
        StatusBarUtils.setStatusBarTransparent(this);
        mMapView = findViewById(R.id.station_query_success_mapView);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setViewPadding(ScreenUtils.dp2px(10), 0, 0, 0);
        BaiduMapUtils.changeDefaultBaiDuMapView(mMapView, mBaiduMap); // 比列尺、放大、缩小按钮、图标等的显示与否
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); // 根据给定增量缩放地图级别
        mBaiduMap.setMapStatus(msu);

        mCollectBtn = findViewById(R.id.station_query_success_collectBtn);
        mCollectBtn2 = findViewById(R.id.station_query_success_collectBtn2);
        mBackImgBtn = findViewById(R.id.station_query_success_back_imgbtn);
        mTrafficImgBtn = findViewById(R.id.station_query_success_traffic_imgbtn);
        mCoverageImgBtn = findViewById(R.id.station_query_success_coverage_imgbtn);
        mLocationImgBtn = findViewById(R.id.location_img_btn);
        mCoverageLayout = findViewById(R.id.station_query_success_coverage_layout);
        m2dMapImgBtn = findViewById(R.id.station_query_success_map_2d_img_btn);
        mSiteMapImgBtn = findViewById(R.id.station_query_success_map_site_img_btn);
        m3dMapImgBtn = findViewById(R.id.station_query_success_map_3d_img_btn);
        mPlaceInfoName = findViewById(R.id.station_query_success_name);
        mPlaceInfoDistance = findViewById(R.id.station_query_success_distance);
        mPlaceInfoAddress = findViewById(R.id.station_query_success_address);
        mZoomIn = findViewById(R.id.zoom_in);
        mZoomOut = findViewById(R.id.zoom_out);
        mContent = findViewById(R.id.station_query_success_content);
        mSlideLayout = findViewById(R.id.station_query_success_slideLayout);
        mBusRecyclerView = findViewById(R.id.station_query_success_recyclerView);

        mBackImgBtn.setOnClickListener(this);
        mCollectBtn.setOnClickListener(this);
        mCollectBtn2.setOnClickListener(this);
        mTrafficImgBtn.setOnClickListener(this);
        m2dMapImgBtn.setOnClickListener(this);
        m3dMapImgBtn.setOnClickListener(this);
        mSiteMapImgBtn.setOnClickListener(this);
        mCoverageImgBtn.setOnClickListener(this);
        mLocationImgBtn.setOnClickListener(this);
        mZoomIn.setOnClickListener(this);
        mZoomOut.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mBaiduMap.setMyLocationEnabled(true);
        myOrientationListener.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mBaiduMap.setMyLocationEnabled(false);
        myOrientationListener.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connection != null) {
            unbindService(connection);
        }
        mMapView.onDestroy();
        mSearch.destroy();
        mBusLineSearch.destroy();
    }

}
